package com.xh.bussiness.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.bean.request.GroupRuleRequest;
import com.xh.bussiness.system.model.GroupRule;
import com.xh.bussiness.system.dao.GroupRuleMapper;
import com.xh.bussiness.system.service.IGroupRuleService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 组权限 服务实现类
 * </p>
 *
 * @author Pitt
 * @since 2020-11-23
 */
@Service
public class GroupRuleServiceImpl extends BaseServiceImpl<GroupRuleMapper, GroupRule> implements IGroupRuleService {
    @Override
    public XhPage queryGroupRulePage(XhPage page, GroupRuleRequest bean) {
        QueryWrapper qw=new QueryWrapper<GroupRule>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryGroupRule(GroupRuleRequest bean) {
        QueryWrapper qw=new QueryWrapper<GroupRule>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public List<GroupRule> queryGroupRuleByGroupId(String groupId,String subjectCode) {
        QueryWrapper<GroupRule> qw = new QueryWrapper<>();
        qw.eq("group_id", groupId);
        if (!StringUtils.isEmpty(subjectCode)) {
            qw.eq("subject_code", subjectCode);
        }
        return this.dao.selectList(qw);
    }

    @Override
    public boolean deleteByGroupId(String groupId,String subjectCode) {
        return this.dao.deleteByGroupId(groupId,subjectCode);
    }

    @Override
    public GroupRule queryGroupRuleBySubjectCodeAndUserId(String subjectCode, String userId) {
        return this.dao.queryGroupRuleBySubjectCodeAndUserId(subjectCode, userId);
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(GroupRuleRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if(!bean.getGroupIds().isEmpty()){
            qw.in("group_id",bean.getGroupIds());
        }
        if (!StringUtils.isEmpty(bean.getSubjectCode())) {
            qw.eq("subject_code", bean.getSubjectCode());
        }
        return qw;
    }
}
